<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>餐厅列表 - 美食社区</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <style>
        .restaurant-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            height: 100%;
        }

        .restaurant-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .restaurant-img-container {
            height: 200px;
            overflow: hidden;
        }

        .restaurant-img-container img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }

        .restaurant-card:hover .restaurant-img-container img {
            transform: scale(1.1);
        }

        .rating-stars {
            color: #ffc107;
        }

        .price-level {
            color: #28a745;
        }

        .filter-section {
            background-color: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 30px;
        }

        .cuisine-badge {
            background-color: #fff3e6;
            color: #fd7e14;
            border: 1px solid #ffd7b5;
            border-radius: 20px;
            padding: 5px 15px;
            margin: 5px;
            display: inline-block;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .cuisine-badge:hover,
        .cuisine-badge.active {
            background-color: #fd7e14;
            color: white;
            border-color: #fd7e14;
        }

        .btn-primary {
            background-color: #fd7e14;
            border-color: #fd7e14;
        }

        .btn-primary:hover {
            background-color: #e76b00;
            border-color: #e76b00;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <nav th:replace="~{common/header :: header}"></nav>

    <div class="container mt-5 pt-5">
        <h2 class="section-title mb-4">探索美食餐厅</h2>

        <!-- 筛选区域 -->
        <div class="filter-section">
            <form id="filterForm" method="get" action="/restaurant">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">菜系类型</label>
                        <select class="form-select" name="cuisineType">
                            <option value="">全部菜系</option>
                            <option value="中餐">中餐</option>
                            <option value="西餐">西餐</option>
                            <option value="日料">日料</option>
                            <option value="韩餐">韩餐</option>
                            <option value="火锅">火锅</option>
                            <option value="烧烤">烧烤</option>
                            <option value="甜点">甜点</option>
                            <option value="面食">面食</option>
                            <option value="海鲜">海鲜</option>
                            <option value="素食">素食</option>
                            <option value="川菜">川菜</option>
                            <option value="粤菜">粤菜</option>
                            <option value="湘菜">湘菜</option>
                            <option value="东南亚菜">东南亚菜</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">价格区间</label>
                        <div class="d-flex">
                            <input type="number" class="form-control me-2" name="minPrice" placeholder="最低价">
                            <input type="number" class="form-control" name="maxPrice" placeholder="最高价">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">最低评分</label>
                        <select class="form-select" name="minRating">
                            <option value="">不限</option>
                            <option value="4.5">4.5分以上</option>
                            <option value="4">4分以上</option>
                            <option value="3.5">3.5分以上</option>
                            <option value="3">3分以上</option>
                        </select>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-search me-2"></i>筛选
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!-- 餐厅列表 -->
        <div class="row g-4" id="restaurantList">
            <div class="col-md-4 mb-4" th:each="restaurant : ${restaurants}">
                <div class="card restaurant-card">
                    <div class="restaurant-img-container">
                        <img th:src="${restaurant.image}" class="card-img-top" th:alt="${restaurant.name}">
                    </div>
                    <div class="card-body">
                        <h5 class="card-title" th:text="${restaurant.name}"></h5>
                        <p class="card-text text-muted" th:text="${restaurant.address}"></p>
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="rating-stars">
                                <span th:text="${restaurant.rating}"></span>
                                <i class="fas fa-star"></i>
                            </div>
                            <div class="price-level">
                                <span th:text="${'¥' + #strings.repeat('¥', restaurant.priceLevel - 1)}"></span>
                            </div>
                        </div>
                        <span class="badge bg-light text-dark mt-2" th:text="${restaurant.cuisineType}"></span>
                    </div>
                    <div class="card-footer bg-white">
                        <a th:href="@{'/restaurant/' + ${restaurant.id}}" class="btn btn-outline-primary w-100">
                            <i class="fas fa-info-circle me-2"></i>查看详情
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- 分页 -->
        <nav aria-label="Page navigation" class="mt-4" th:if="${totalPages > 1}">
            <ul class="pagination justify-content-center">
                <li class="page-item" th:classappend="${currentPage == 1 ? 'disabled' : ''}">
                    <a class="page-link"
                        th:href="@{/restaurant(page=${currentPage - 1}, cuisineType=${param.cuisineType}, minPrice=${param.minPrice}, maxPrice=${param.maxPrice}, minRating=${param.minRating})}"
                        aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <li class="page-item" th:each="i : ${#numbers.sequence(1, totalPages)}"
                    th:classappend="${i == currentPage ? 'active' : ''}">
                    <a class="page-link"
                        th:href="@{/restaurant(page=${i}, cuisineType=${param.cuisineType}, minPrice=${param.minPrice}, maxPrice=${param.maxPrice}, minRating=${param.minRating})}"
                        th:text="${i}"></a>
                </li>
                <li class="page-item" th:classappend="${currentPage == totalPages ? 'disabled' : ''}">
                    <a class="page-link"
                        th:href="@{/restaurant(page=${currentPage + 1}, cuisineType=${param.cuisineType}, minPrice=${param.minPrice}, maxPrice=${param.maxPrice}, minRating=${param.minRating})}"
                        aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
    </div>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

    <script th:replace="~{common/header :: logout-script}"></script>

    <script>
        $(document).ready(function () {
            // 设置筛选表单的初始值
            const urlParams = new URLSearchParams(window.location.search);
            $('select[name="cuisineType"]').val(urlParams.get('cuisineType') || '');
            $('input[name="minPrice"]').val(urlParams.get('minPrice') || '');
            $('input[name="maxPrice"]').val(urlParams.get('maxPrice') || '');
            $('select[name="minRating"]').val(urlParams.get('minRating') || '');

            // 快速菜系筛选
            $('.cuisine-badge').click(function () {
                const cuisine = $(this).data('cuisine');
                $('.cuisine-badge').removeClass('active');
                $(this).addClass('active');
                $('select[name="cuisineType"]').val(cuisine);
                $('#filterForm').submit();
            });

            // 高亮当前选中的菜系
            const currentCuisine = urlParams.get('cuisineType') || '';
            $('.cuisine-badge').removeClass('active');
            $(`.cuisine-badge[data-cuisine="${currentCuisine}"]`).addClass('active');
            if (currentCuisine === '' || currentCuisine === null) {
                $('.cuisine-badge[data-cuisine=""]').addClass('active');
            }
        });
    </script>
</body>

</html>